<?php 
/**
 * Sync client entrance
 *
 * @desc DIANBOOM.com sync client entrance
 * @author Andrew lee <andrew(at)dianboom(dot)com>
 * @since 11:27 2011/07/15
 * @copyright 2010-2011 dianboom corporation. All rights reserved.
 */

/**
 * @desc define SYNC_CLIENT_ROOT
 */
define('SYNC_CLIENT_ROOT', realpath(dirname(__FILE__)). DIRECTORY_SEPARATOR);

/**
 * @desc include path
 */
define('INCLUDE_ROOT', SYNC_CLIENT_ROOT. 'includes'. DIRECTORY_SEPARATOR);

/**
 * @desc include configure
 */
$config = require INCLUDE_ROOT. 'config.inc.php';
$config['methods'][] = 'token';

/**
 * @desc define VERSION
 */
define('VERSION', isset($_GET['version']) && $_GET['version']? trim($_GET['version']): $config['version']);

/**
 * @desc module path
 */
define('MODULE_ROOT', SYNC_CLIENT_ROOT. 'modules'. DIRECTORY_SEPARATOR);

/**
 * @desc version path
 */
define('VERSION_ROOT', MODULE_ROOT. 'v'. VERSION. DIRECTORY_SEPARATOR);

/**
 * @desc load json compat package
 */
require INCLUDE_ROOT. 'json.php';

if (
    function_exists('get_magic_quotes_gpc') &&
    get_magic_quotes_gpc() &&
    function_exists('stripslashes')
) {
    $_GET   = array_map('stripslashes',  $_GET);
    $_POST  = array_map('stripslashes',  $_POST);
}

/**
 * @desc check method
 */
isset($_GET['action']) || die(json_encode(array(-1, 'Invalid method')));

in_array($_GET['action'], $config['methods']) || die(json_encode(array(-1, 'Invalid method')));

/**
 * ===============================================
 * for testing
 * ===============================================
 
$_POST = $_GET;

unset($_POST['action']);

ksort($_POST);

$_POST = array_merge(
	$_POST,
	array('key' => md5($config['email'].'|'.$config['token'].'|'.http_build_query($_POST, '', '&')))
);
// ===============================================*/

/**
 * Include common interface & class
 */
require MODULE_ROOT. 'SYNC_CLIENT_INT.php';

require MODULE_ROOT. 'SYNC_CLIENT.class.php';

/**
 * Include current version interface
 */
require VERSION_ROOT. 'interface.php';

/**
 * @desc load class
 */
file_exists($moduleFile = VERSION_ROOT. $config['db']['db_type']. '_client.class.php') &&
    (require $moduleFile) ||
    die(json_encode(array(-1, 'Module file not exists')));

class_exists($className = $config['db']['db_type'].'_client') ||
    die(json_encode(array(-1, "Unkown class $className")));

/**
 * @desc intialize object
 */
$client = new $className($config, $_GET['action']);

/**
 * @desc run method
 */
$client->run();
?>